const db = require('../db/index')

// 获取用户信息列表
exports.userList = (req, res) => {
  // 每页几条数据
  let pagesize = 5
  // 当前页码数
  let pagenum = (req.query.pagenum - 1) * pagesize
  let datas = []
  const sql = `select uid,account,phone,create_time from user limit ${pagenum},${pagesize}`
  const sql2 = `select count(*) as total from user `
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取用户信息列表失败！')
    ;(datas = results),
      db.query(sql2, (err, result) => {
        if (err) return res.cc(err)
        if (result.length < 0) return res.cc('获取用户信息列表失败！')
        res.send({
          status: 0,
          message: '获取用户信息列表成功！',
          data: datas,
          total: result[0]
        })
      })
  })
}

// 更新用户信息列表
exports.updateUserList = (req, res) => {
  const sql = `update user set account = ?,phone = ? where uid = ?`
  db.query(sql, [req.body.account, req.body.phone, req.body.uid], (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('更新用户信息列表失败！')
    res.send({
      status: 0,
      message: '更新用户信息列表成功！'
    })
  })
}
